<?php
namespace app\custom_user\controller;

use cmf\controller\HomeBaseController;
use app\custom_user\logic\UserLogic;
use app\custom_user\model\UserSession;

/**
 * PC端登录和注册页面
 */
class PublicController extends HomeBaseController
{
    /**
     * 登陆后跳转到session中指定的链接
     */
    protected function loginRedirect($msg="")
    {
        $url=session("login_redirect_url");
        session("login_redirect_url",null);
        if(!$url){
            $url=url("custom_user/index/index");
        }
        if($msg){
            $this->success($msg,$url);
        }
        else{
            $this->redirect($url);
        }
    }
    public function register()
    {
        return $this->fetch("register");
    }
    public function doRegister()
    {
        $ul=new UserLogic;
        $user=$ul->register($_REQUEST);
        if(!$user){
            $this->error($ul->getError());
        }
        $this->loginRedirect("注册成功，正在跳转");
    }
    public function login()
    {
        return $this->fetch("login");
    }
    public function doLogin()
    {
        $ul=new UserLogic;
        $user=$ul->login($_REQUEST);
        if(!$user){
            $this->error($ul->getError());
        }
        $this->loginRedirect("登陆成功，正在跳转");
    }
}
